<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue框架介绍和原理/code/lib/vue.global.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ul,ol,li{
            list-style: none;
        }
        li{
            display: flex;
            justify-content: space-around;
            padding: 10px;
        }
        li img{
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div id="box">
        全选按钮：<input type="checkbox" v-model="isAll" @change="handleAllChecked">
        <ul>
            <li v-for="(item, index) in dataList" :key="item.id">
                <input type="checkbox" v-model="checkList" :value="item" @change="handleItemChecked">
                <img :src="item.pic" alt="">

                <div>
                    <div>{{item.name}}</div>
                    <div style="color: red;">￥{{item.price}}</div>
                </div>

                <div>
                    <button @click="item.number--" :disabled="item.number===1">-</button>
                    <span>{{item.number}}</span>
                    <button @click="item.number++" :disabled="item.number===item.limit">+</button>
                </div>

                <div>
                    <button @click="handleDel(index,item.id)">删除</button>
                </div>
            </li>
            <div>总金额：{{ computedSum }}</div>
            {{checkList}}
        </ul>
    </div>
    <script>
          var obj ={
            data(){
                return{
                    isAll:false,
                    checkList:[],
                    dataList:[
               {
                   name:'商品1',
                   price:10,
                   number:1,
                   id:1,
                   limit:5,
                   pic:"https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg"
               },
               {
                   name:'商品2',
                   price:20,
                   number:2,
                   id:2,
                   limit:10,
                   pic:"https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg" 
               },
               {
                   name:'商品3',
                   price:30,
                   number:3,
                   id:3,
                   limit:15,
                   pic:"https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg" 
               }
           ]
        }
    },
    methods: {
           handleDel(index,id){
            this.dataList.splice(index,1)

            this.checkList=this.checkList.filter(item=>item.id!==id)

            this.handleItemChecked()
           },
           handleAllChecked(){
            if(this.isAll){
                this.checkList=this.dataList
            }else{
                this.checkList=[]
            }
           },
           handleItemChecked(){
            if(this.checkList.length===this.dataList.length){
                this.isAll=true
            }else{
                this.isAll=false
            }
           },

       },
       computed:{
        computedSum(){
            var total=0
               this.checkList.forEach(item => {
                   total+=item.price*item.number
               })
               return total
        }
       }
}    
        Vue.createApp(obj).mount('#box')
    </script>
</body>
</html>